﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net.Sockets;

namespace InfoFlash
{
    [Serializable]
    public class MessageData
    {
        public string Code { get; set; }
        public string Message { get; set; }
        public object Object { get; set; }

        public static byte[] ToByteArray(object source)
        {
            var formatter = new BinaryFormatter();
            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, source);
                return stream.ToArray();
            }
        }


        public static MessageData Deserialize(NetworkStream stream)
        {
            try{
                BinaryFormatter bf = new BinaryFormatter();
                return (MessageData)bf.Deserialize(stream);
            }
            catch{            
                return new MessageData();            
            }
        }

     
    }

}